#include "stdafx.h"
#include "SpriteGate.h"
#include "SpriteSingleFrame.h"
#include "SpriteMultiFrame.h"
#include "SpriteCombined.h"
#include "XMLManager.h"
#include "DisplayController.h"
#include "..\LogicSprites\LgSpriteGate.h"

SpriteGate::SpriteGate(	const BalloonElemnet_t& stBalloonElemnet,int nLayer ):BaseDispSprite(nLayer)
{
	vector<BaseDispSprite*> listPassSprites;
	vector<BaseDispSprite*> listDenySprites;
	vector<BaseDispSprite*> listOtherSprites;//always draw
	list<PicInBalloonElemnet_t>::const_iterator itPic=stBalloonElemnet.listPic.begin();
	for (;itPic!=stBalloonElemnet.listPic.end();itPic++){
		Pic_t stPic=XMLManager::m_vecPic[itPic->TypeID-1][itPic->ID-1];

		LPDIRECT3DTEXTURE9 pTexture=DisplayController::CreateTex(PicIdx_t(itPic->TypeID-1,itPic->ID-1),stPic);
#ifndef TEST_SFP			
		if(NULL==pTexture) continue;
#endif			
		BaseDispSprite* pSprite=NULL;
		if (1==stPic.nFrameNum*stPic.nLine){
			pSprite=new SpriteSingleFrame(pTexture,stPic.nWidth,stPic.nHeight,nLayer);
		}else{
			pSprite=new SpriteMultiFrame(pTexture,
				stPic.nFrameNum,stPic.nWidth,stPic.nHeight,
				stPic.nFrameSpan,stPic.nFrameNum*stPic.nLine,nLayer);
		}
#ifdef TEST_SFP
		pSprite->SetString(stPic.csFile);
#endif

		D3DXVECTOR2 pos((float)itPic->x,(float)itPic->y);
		pSprite->m_vPosition=pos;
		if (itPic->Type==2){
			listPassSprites.push_back(pSprite);
		}
		if (itPic->Type==1){
			listDenySprites.push_back(pSprite);
		}
		if (itPic->Type==0){
			listOtherSprites.push_back(pSprite);
		}
	}
	m_pPassSprite=new SpriteCombined(listPassSprites,nLayer);
	m_pDenySprite=new SpriteCombined(listDenySprites,nLayer);
	m_pOtherSprite=new SpriteCombined(listOtherSprites,nLayer);
}


SpriteGate::~SpriteGate(void)
{
	SAFE_DELETE(m_pPassSprite)
	SAFE_DELETE(m_pOtherSprite)
	SAFE_DELETE(m_pDenySprite)
}

bool SpriteGate::Draw( DWORD dwCurrentTime )
{
	BaseDispSprite::UpdatePosByLgSprite();
	m_pPassSprite->m_vPosition=m_vPosition;
	m_pDenySprite->m_vPosition=m_vPosition;
	m_pOtherSprite->m_vPosition=m_vPosition;
	LgSpriteGate* pLg=dynamic_cast<LgSpriteGate*>(m_pLgSprite);
	if (pLg->m_bPass){
		m_pPassSprite->Draw(dwCurrentTime);
	}else{
		m_pDenySprite->Draw(dwCurrentTime);
	}
	m_pOtherSprite->Draw(dwCurrentTime);
	return true;
}
